package com.coocaa.x.service.liteplayer;

import android.content.Context;
import android.content.Intent;
import android.os.RemoteException;
import android.util.Log;

import com.coocaa.x.framework.utils.LogUtils;
import com.coocaa.x.service.liteplayer.aidl.ILitePlayer;
import com.coocaa.x.service.liteplayer.aidl.ILitePlayerKernelProxyService;
import com.coocaa.x.service.liteplayer.aidl.ILitePlayerListener;
import com.coocaa.x.service.liteplayer.aidl.ILitePlayerMonitor;
import com.coocaa.x.service.liteplayer.aidl.PlayerKernelInfo;
import com.coocaa.x.service.liteplayer.aidl.ReadyInfo;
import com.coocaa.x.service.liteplayer.aidl.VideoSourceInfo;

/**
 * Created by lu on 15-7-29.
 */
public class LitePlayerKernelServiceStub extends ILitePlayerKernelProxyService.Stub {
    private static final String tag = "kernelservice";

    private class MyPlayer extends ILitePlayer.Stub {

        @Override
        public void errorOccurred(String arg0, String arg1) throws RemoteException {
            // TODO Auto-generated method stub
            if (kernelPlayer != null)
                kernelPlayer.errorOccurred(arg0, arg1);
        }

        @Override
        public int getCurrentTimeInMillis() throws RemoteException {
            // TODO Auto-generated method stub
            if (kernelPlayer != null)
                return kernelPlayer.getCurrentTimeInMillis();
            return 0;
        }

        @Override
        public int getDuration() throws RemoteException {
            // TODO Auto-generated method stubgetCurrentTimeInMillis
            if (kernelPlayer != null)
                return kernelPlayer.getDuration();
            return 0;
        }

        @Override
        public void getReady(ReadyInfo arg0) throws RemoteException {
            // TODO Auto-generated method stub
            // start playerkernel wait until kernelPlayer != null
            LogUtils.d(tag, "getReay  name:" + arg0.name);
            if (kernelPlayer == null) {
                startPlayerActivity();
                while (kernelPlayer == null)
                    try {
                        Thread.currentThread().sleep(10);
//                            LogUtils.d(tag, "getReady   not connected !!!");
                    } catch (Exception e) {
                        // TODO: handle exception
                        LogUtils.d(tag, "not connected !!!error e:" + e);
                        e.printStackTrace();
                    }
            }
            if (kernelPlayer != null) {
                LogUtils.d(tag, "getReay  kernelPlayer is not null");
                kernelPlayer.getReady(arg0);
            }

        }

        @Override
        public boolean isPlaying() throws RemoteException {
            // TODO Auto-generated method stub
            if (kernelPlayer != null)
                return kernelPlayer.isPlaying();
            return false;
        }

        @Override
        public boolean pause() throws RemoteException {
            // TODO Auto-generated method stub
            if (kernelPlayer != null)
                return kernelPlayer.pause();
            return false;
        }

        @Override
        public boolean resume() throws RemoteException {
            // TODO Auto-generated method stub
            if (kernelPlayer != null)
                return kernelPlayer.resume();
            return false;
        }

        @Override
        public boolean seekTo(int arg0) throws RemoteException {
            // TODO Auto-generated method stub
            if (kernelPlayer != null)
                return kernelPlayer.seekTo(arg0);
            return false;
        }

        @Override
        public boolean start(VideoSourceInfo arg0, String arg1) throws RemoteException {
            if (kernelPlayer != null)
                return kernelPlayer.start(arg0, arg1);
            return false;
        }

        @Override
        public boolean stop() throws RemoteException {
            if (kernelPlayer != null)
                return kernelPlayer.stop();
            return false;
        }
    }

    private void startPlayerActivity() {
        LogUtils.d("ps", "startPlayerActivity    PlayerActivity");
        Intent intent = new Intent();
        intent.setAction("com.coocaa.x.service.player");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        try {
            mContext.startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private class MyMonitor extends ILitePlayerMonitor.Stub {

        @Override
        public void addPlayerListener(ILitePlayerListener arg0) throws RemoteException {
            clientServiceListener = arg0;
        }

        @Override
        public void removePlayerListener(ILitePlayerListener arg0) throws RemoteException {
            clientServiceListener = null;
        }
    }

    private class MyListener extends ILitePlayerListener.Stub {

        @Override
        public void onBufferingUpdate(int arg0) throws RemoteException {
            if (clientServiceListener != null)
                clientServiceListener.onBufferingUpdate(arg0);
        }

        @Override
        public void onCompletion() throws RemoteException {
            if (clientServiceListener != null)
                clientServiceListener.onCompletion();
        }

        @Override
        public boolean onError(int arg0, int arg1) throws RemoteException {
            // TODO Auto-generated method stub
            if (clientServiceListener != null)
                return clientServiceListener.onError(arg0, arg1);
            return false;
        }

        @Override
        public boolean onInfo(int arg0, int arg1) throws RemoteException {
            // TODO Auto-generated method stub
            if (clientServiceListener != null)
                return clientServiceListener.onInfo(arg0, arg1);
            return false;
        }

        @Override
        public void onPlayPaused() throws RemoteException {
            // TODO Auto-generated method stub
            if (clientServiceListener != null)
                clientServiceListener.onPlayPaused();
        }

        @Override
        public void onPlayResumed() throws RemoteException {
            // TODO Auto-generated method stub
            if (clientServiceListener != null)
                clientServiceListener.onPlayResumed();
        }

        @Override
        public void onPlayStarted(String arg0, int arg1) throws RemoteException {
            // TODO Auto-generated method stub
            if (clientServiceListener != null)
                clientServiceListener.onPlayStarted(arg0, arg1);
        }

        @Override
        public void onPlayStopped() throws RemoteException {
            // TODO Auto-generated method stub
            if (clientServiceListener != null)
                clientServiceListener.onPlayStopped();
        }

        @Override
        public void onPrepared() throws RemoteException {
            // TODO Auto-generated method stub
            if (clientServiceListener != null)
                clientServiceListener.onPrepared();
        }

        @Override
        public void onProgressChanged(int arg0) throws RemoteException {
            // TODO Auto-generated method stub
            if (clientServiceListener != null)
                clientServiceListener.onProgressChanged(arg0);
        }

    }

    private ILitePlayerListener clientServiceListener = null;

    private MyPlayer myPlayer = null;
    private MyMonitor myMonitor = null;
    private MyListener myListener = null;

    private ILitePlayer kernelPlayer = null;
    private ILitePlayerMonitor kernelMonitor = null;
    private Context mContext = null;

    public LitePlayerKernelServiceStub(Context context) {
        mContext = context;
        myPlayer = new MyPlayer();
        myMonitor = new MyMonitor();
        myListener = new MyListener();
    }

    @Override
    public ILitePlayer getPlayer() throws RemoteException {
        return myPlayer;
    }

    @Override
    public ILitePlayerMonitor getPlayerMonitor() throws RemoteException {
        return myMonitor;
    }

    @Override
    public void setPlayerKernelMonitor(ILitePlayerMonitor arg0) throws RemoteException {
        kernelMonitor = arg0;
        kernelMonitor.addPlayerListener(myListener);
    }

    @Override
    public void setPlayerKernel(ILitePlayer arg0, PlayerKernelInfo arg1) throws RemoteException {
        // TODO Auto-generated method stub
        kernelPlayer = arg0;
    }

    @Override
    public void clearPlayerKernel() throws RemoteException {
        // TODO Auto-generated method stub
        kernelPlayer = null;
    }

    @Override
    public void clearPlayerKernelMonitor() throws RemoteException {
        // TODO Auto-generated method stub
        kernelMonitor = null;
    }


    @Override
    public void whoAmI() {
        LogUtils.d("wai", "<<LitePlayerKernelService>> who am i??  " + mContext.getPackageName());
    }
}
